package com.ytzl.gotrip.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ytzl.gotrip.ext.utils.RedisUtils;
import com.ytzl.gotrip.model.GotripUser;
import com.ytzl.gotrip.model.GotripUserLinkUser;
import com.ytzl.gotrip.rpc.api.RpcGotripUserLinkUserService;
import com.ytzl.gotrip.rpc.api.RpcTokenService;
import com.ytzl.gotrip.service.GotripUserLinkUserService;
import com.ytzl.gotrip.vo.userinfo.ItripModifyUserLinkUserVO;
import com.ytzl.gotrip.vo.userinfo.ItripSearchUserLinkUserVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service("gotripUserLinkUserService")
public class GotripUserLinkUserServiceImpl implements GotripUserLinkUserService {
    @Reference
    private RpcGotripUserLinkUserService rpcGotripUserLinkUserService;
    @Reference
    RpcTokenService rpcTokenService;
    @Override
    public List<GotripUserLinkUser> getUserLinkUserByUserId(ItripSearchUserLinkUserVO itripSearchUserLinkUserVO, String token, String Agent) throws Exception {
        GotripUser gotripUser = rpcTokenService.getGotripUser(token, Agent);

        Map<String,Object>  map = new HashMap<>() ;
        map.put("userId",gotripUser.getId());
        map.put("linkUserName",itripSearchUserLinkUserVO.getLinkUserName());

        return rpcGotripUserLinkUserService.getGotripUserLinkUserListByMap(map);
    }

    @Override
    public void delUserLinkUserById(Long ids, String token, String agent) throws Exception {
        GotripUser gotripUser = rpcTokenService.getGotripUser(token, agent);
        rpcGotripUserLinkUserService.deleteGotripUserLinkUserById(ids);
    }

    @Override
    public void modifyUserLinkUser(ItripModifyUserLinkUserVO itripModifyUserLinkUserVO, String token, String agent) throws Exception {
        GotripUser gotripUser = rpcTokenService.getGotripUser(token, agent);
        GotripUserLinkUser itripUserLinkUser = new GotripUserLinkUser();
        if(null != gotripUser && null != itripModifyUserLinkUserVO) {

            itripUserLinkUser.setId(itripModifyUserLinkUserVO.getId());
            itripUserLinkUser.setLinkUserName(itripModifyUserLinkUserVO.getLinkUserName());
            itripUserLinkUser.setLinkIdCardType(itripModifyUserLinkUserVO.getLinkIdCardType());
            itripUserLinkUser.setLinkIdCard(itripModifyUserLinkUserVO.getLinkIdCard());
            String id = gotripUser.getId() + "";
            itripUserLinkUser.setUserId(Integer.parseInt(id));
            itripUserLinkUser.setLinkPhone(itripModifyUserLinkUserVO.getLinkPhone());
            itripUserLinkUser.setModifiedBy(gotripUser.getId());
            //itripUserLinkUser.setModifyDate(new Date());
        }
        itripUserLinkUser.setModifyDate(new Date());
        rpcGotripUserLinkUserService.updateGotripUserLinkUser(itripUserLinkUser);
    }

    @Override
    public void addUserLinkUser(ItripModifyUserLinkUserVO itripModifyUserLinkUserVO, String token, String agent) throws Exception {

        GotripUser gotripUser = rpcTokenService.getGotripUser(token, agent);
        GotripUserLinkUser itripUserLinkUser = new GotripUserLinkUser();
        if(null != gotripUser && null != itripModifyUserLinkUserVO){
            itripUserLinkUser.setLinkUserName(itripModifyUserLinkUserVO.getLinkUserName());
            itripUserLinkUser.setLinkIdCardType(itripModifyUserLinkUserVO.getLinkIdCardType());
            itripUserLinkUser.setLinkIdCard(itripModifyUserLinkUserVO.getLinkIdCard());
            Integer id = Integer.parseInt(gotripUser.getId()+"");
            itripUserLinkUser.setUserId(id);
            itripUserLinkUser.setLinkPhone(itripModifyUserLinkUserVO.getLinkPhone());
            itripUserLinkUser.setCreatedBy(gotripUser.getId());
            itripUserLinkUser.setCreationDate(new Date(System.currentTimeMillis()));

        }


        rpcGotripUserLinkUserService.insertGotripUserLinkUser(itripUserLinkUser);
    }
}
